//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package oldClasses;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class MyInteractiveForm extends JPanel {
	private ArrayList<String> fields;

	protected JTable table;
	protected JScrollPane scroller;
	protected MyTableModel_OLD tableModel;

	public MyTableModel_OLD getTableModel() {
		return tableModel;
	}

	public void setTableModel(MyTableModel_OLD tableModel) {
		this.tableModel = tableModel;
	}

	public MyInteractiveForm() {
		initComponent();
	}

	public MyInteractiveForm(ArrayList<String> fields) {
		this.fields = fields;
		initComponent();
	}

	public void initComponent() {

		tableModel = new MyTableModel_OLD(fields);

		tableModel
				.addTableModelListener(new MyInteractiveForm.InteractiveTableModelListener());
		table = new JTable();
		table.setModel(tableModel);
		table.setSurrendersFocusOnKeystroke(true);
		table.setEnabled(false);
		if (!tableModel.hasEmptyRow()) {
			tableModel.addEmptyRow();
		}
		// table.getRowSelectionInterval(0,0);
		/*
		 * table.addKeyListener(new KeyListener(){
		 * 
		 * public void keyPressed(KeyEvent e) { // TODO Auto-generated method
		 * stub //table.setBackground(Color.YELLOW);
		 * 
		 * //int i=table.getEditingRow();
		 * e.getComponent().setBackground(Color.YELLOW);
		 * 
		 * 
		 * }
		 * 
		 * public void keyReleased(KeyEvent e) { // TODO Auto-generated method
		 * stub table.setBackground(Color.YELLOW); }
		 * 
		 * public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub
		 * table.setBackground(Color.YELLOW); }
		 * 
		 * });
		 */
		scroller = new javax.swing.JScrollPane(table);
		table.setPreferredScrollableViewportSize(new java.awt.Dimension(500,
				300));
		
		 TableColumn hidden =
		  table.getColumnModel().getColumn(TableModel.HIDDEN_INDEX);
		 hidden.setMinWidth(40); hidden.setPreferredWidth(40);
		 hidden.setMaxWidth(40); hidden.setCellRenderer(new
		 InteractiveRenderer(TableModel.HIDDEN_INDEX));
		 

		setLayout(new BorderLayout());
		add(scroller, BorderLayout.CENTER);
	}

	public void highlightLastRow(int row) {
		int lastrow = tableModel.getRowCount();
		if (row == lastrow - 1) {
			table.setRowSelectionInterval(lastrow - 1, lastrow - 1);
		} else {
			table.setRowSelectionInterval(row + 1, row + 1);
		}

		table.setColumnSelectionInterval(0, 0);
	}

	class InteractiveRenderer extends DefaultTableCellRenderer {
		protected int interactiveColumn;

		public InteractiveRenderer(int interactiveColumn) {
			this.interactiveColumn = interactiveColumn;
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			Component c = super.getTableCellRendererComponent(table, value,
					isSelected, hasFocus, row, column);
			if (column == interactiveColumn && hasFocus) {
				if ((MyInteractiveForm.this.tableModel.getRowCount() - 1) == row
						&& !MyInteractiveForm.this.tableModel.hasEmptyRow()) {
					MyInteractiveForm.this.tableModel.addEmptyRow();
					MyInteractiveForm.this.setBackground(Color.GREEN);
					MyInteractiveForm.this.setForeground(Color.RED);
				}

				highlightLastRow(row);
			}

			return c;
		}
	}

	public class InteractiveTableModelListener implements TableModelListener {
		public void tableChanged(TableModelEvent evt) {
			if (evt.getType() == TableModelEvent.UPDATE) {
				int column = evt.getColumn();
				int row = evt.getFirstRow();
				System.out.println("row: " + row + " column: " + column);
				/*
				 * table.setColumnSelectionInterval(column + 1, column + 1);
				 * table.setRowSelectionInterval(row, row);
				 */
			}
		}
	}

	/*
	 * public static void main(String[] args) { try {
	 * UIManager.setLookAndFeel(UIManager
	 * .getCrossPlatformLookAndFeelClassName()); JFrame frame = new
	 * JFrame("Interactive Form"); frame.addWindowListener(new WindowAdapter() {
	 * public void windowClosing(WindowEvent evt) { System.exit(0); } });
	 * frame.getContentPane().add(new InteractiveForm()); frame.pack();
	 * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 */
}
